<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    
</body>
<script>
    let str='abc';
        switch(str){
        case 'abc':
            console.log('abc')
            break;
        case 'bbc':
            console.log('bbc')
            break;
        default:
            console.log('都未匹配上！')
    }

    // JS 支持 switch 的表达式为布尔类型值
    let o = false;
    switch(o){
        case true:
            console.log(true)
            break;
        case false:
            console.log(false)
            break;
        default:
            console.log('default')
            break;
    }





    //练习
    console.log(" -------1. 判断一个年份是闰年还是平年----------")
    function isLeapYear(year) {
        // 能被4整除但不能被100整除，或者能被400整除
        if ((year % 4 == 0 && year % 100 !== 0) || (year % 400 == 0)) {
            console.log(`${year} 年是闰年`);
            
        } else {
            console.log(`${year} 年是平年`);
        
        }
    }
    isLeapYear(2020); 
    isLeapYear(1900); 
    isLeapYear(2000); 

    console.log(" -------2. 判断一个人的年龄是否满18岁（是否成年）----------")
    function isAdult(age) {
        if (age >= 18) {
            console.log(`年龄 ${age}：已成年`);
        } else {
            console.log(`年龄 ${age}：未成年`);
        }
    }
    isAdult(20); 
    isAdult(16); 

    console.log(" -------3. switch 分数转换，把百分制转换成 ABCDE----------")
    function grade(score) {
    let grade;
    switch (true) {
        case (score >=90):
            grade = 'A';
        break;
        case (score >=80):
            grade = 'B';
        break;
        case (score >=70):
            grade = 'C';
        break;
        case (score >= 60):
            grade = 'D';
        break;
        default: 
            grade = 'E';
    }
    console.log(`分数 ${score}:等级 ${grade}`);
    }
    grade(55);
    grade(68); 
    grade(75); 
    grade(85);
    console.log(" -------4. switch 输入 1~7 的数组，显示星期几----------")
    function numToweekday(arr) {
    let result = [];            
    for (let i = 0; i < arr.length; i++) {
        let num = arr[i];
        let day;
        switch (num) {
        case 1:
            day = '星期一';
            break;
        case 2:
            day = '星期二';
            break;
        case 3:
            day = '星期三';
            break;
        case 4:
            day = '星期四';
            break;
        case 5:
            day = '星期五';
            break;
        case 6:
            day = '星期六';
            break;
        case 7:
            day = '星期日';
            break;
        default:
            day = '输入不合法';
        }
        result.push(day);
    }
    return result;
    }
    let nums = [1, 3, 5, 7, 8];
    let weekdays = numToweekday(nums);
    for(let i=0;i<nums.length;i++){
        console.log(nums[i],':',weekdays[i])
    }
    


   

</script>
</html>